package com.mars.manager.web.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.mars.manager.user.entity.User;
import com.mars.manager.user.exception.UserBizException;
import com.mars.manager.user.service.UserService;
import com.mars.web.common.pojo.DataGrid;
import com.mars.web.common.pojo.ResponseResult;
import com.mars.web.common.support.BaseController;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	
	@Autowired
	private UserService service ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}

	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@ApiOperation(value="添加用户", notes="", response=ResponseResult.class)
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public Object add(@ApiParam(value="user实体bean", required=true)User entity) {
		try {
			entity.setPassword(UUID.randomUUID().toString());
			entity.setSalt(UUID.randomUUID().toString());
			this.service.insertSelective(entity) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		
		return setResponseResultAddOk();
	}
	
	@ApiOperation(value="删除用户", notes="根据用户ID来指定删除用户，可批量删除", response=ResponseResult.class)
	@ApiImplicitParam(name="ids", value="用户ID，删除多个用户用“,”分割", dataType="String")
	@ResponseBody
	@RequestMapping(value="/delete/{ids}", method=RequestMethod.GET)
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultDeleteOk();
	}
	
	@ApiOperation(value="修改用户", notes="根据用户ID来指定修改对象", response=ResponseResult.class)
	@ApiImplicitParam(name="user", value="user的实体bean，ID不能为空", dataType="User")
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public Object update(User entity) {
		try {
			this.service.updateSelective(entity) ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
		return setResponseResultUpdateOk();
	}
	
	@ApiOperation(value="用户详细信息", notes="根据用户ID来获取用户详细信息", response=User.class)
	@ResponseBody
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public User get(@PathVariable Long id) {
		return this.service.selectById(id) ;
	}
	
	@ApiOperation(value="用户列表", notes="", response=DataGrid.class)
	@ResponseBody
	@RequestMapping(value="/list", method={RequestMethod.GET, RequestMethod.POST})
	public Object list(
			@ApiParam(value="用户实体对象，用户属性作为筛选条件")User entity,
			@ApiParam(value="当前页码")Integer page,
			@ApiParam(value="每页显示多少条记录")Integer rows
	) {
		try {
			PageInfo<User> selectPage = this.service.selectPage(getPageParam(page, rows), entity, getPageParam().getOrderby()) ;
			DataGrid<User> dg = new DataGrid<User>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (UserBizException e) {
			return setResponseResultFailed(e.getMessage());
		}
	}
	
}
